package com.smartHome.controller;

import com.smartHome.pojo.entity.Device;
import com.smartHome.pojo.entity.Result;
import com.smartHome.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/device")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    @PostMapping("/add")
    public Result<Void> add(@RequestBody Device device) {
        deviceService.add(device);
        return Result.success();
    }

    /**
     * 删除房间指定的设备
     * 
     * @param deviceId
     * @return
     */
    @GetMapping("/delete/{deviceId}")
    public Result<Void> delete(@PathVariable Integer deviceId) {
        deviceService.delete(deviceId);
        return Result.success();
    }

    /**
     * 通过房间id查找所有设备
     * 
     * @param roomId
     * @return
     */
    @GetMapping("/getDeviceList/{roomId}")
    public Result<List<Device>> getDeviceList(@PathVariable Integer roomId) {
        List<Device> deviceList = deviceService.getDeviceList(roomId);
        return Result.success(deviceList);
    }

    /**
     * 修改设备状态
     * 
     * @param device
     * @return
     */
    @PostMapping("/updateDevice")
    public Result<Void> updateDevice(@RequestBody Device device) {
        deviceService.update(device);
        return Result.success();
    }

    /**
     * 根据设备Id获取设备详细信息
     * @param deviceId
     * @return
     */
    @GetMapping("/getDevice/{deviceId}")
    public Result<Device> getDevice(@PathVariable Integer deviceId){
        Device device = deviceService.getDevice(deviceId);
        return  Result.success(device);
    }

    /**
     * 关闭房间内所有设备
     * 
     * @param roomId 房间ID
     * @return
     */
    @GetMapping ("/turnOffAll/{roomId}")
    public Result<Void> turnOffAllDevices(@PathVariable Integer roomId) {
        deviceService.turnOffAllDevices(roomId);
        return Result.success();
    }

    /**
     * 根据房屋Id关闭房间内所有设备
     * 
     * @param houseId 房屋ID
     * @return
     */
    @GetMapping("/turnOffByHouse/{houseId}")
    public Result<Void> turnOffDevicesByHouseId(@PathVariable Integer houseId) {
        deviceService.turnOffDevicesByHouseId(houseId);
        return Result.success();
    }
}
